feat: add Query Builder lockForUpdate()#10171
Conversation
2945994 to
b7737be
Compare
michalsn
left a comment
There was a problem hiding this comment.
For Postgre we should have checks for distinct(), groupBy(), having(), aggregation functions, and union().
https://www.postgresql.org/docs/current/sql-select.html
For MySQLi we should check for union(). We should also verify if distinct()/groupBy()/aggregates are the problem, because I couldn't find any docs for this.
https://dev.mysql.com/doc/refman/9.7/en/innodb-locking-reads.html
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
- add driver validation for Postgre and OCI8 unsupported query shapes - reject lockForUpdate with union queries across supported builders - track Query Builder aggregate helper selections for lock validation - document row-locking restrictions and add focused builder coverage Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
0fdfe9c to
026bcb7
Compare
Thanks for the pointers. I added the checks for Postgre and OCI8, and kept MySQLi limited to |
Description
This PR proposes adding
lockForUpdate()to the Query Builder for pessimistic write locking on supported database drivers.The goal is to provide a small framework-native API for code that needs to read rows and then safely update them inside the same transaction.
Example:
Why
Some application workflows need to prevent concurrent writes around the same row while a transaction is deciding what to do next.
Common examples include:
Without a Query Builder method, users either need raw SQL per database driver or custom helper code around otherwise builder-based queries.
Behavior
For drivers that support a trailing
FOR UPDATEclause,lockForUpdate()appends it to the compiledSELECT.SQLSRV is handled separately because SQL Server does not use trailing
FOR UPDATE. Instead, it appliesWITH (UPDLOCK, ROWLOCK)table hints to table references in theFROMclause.Supported drivers in this PR:
Unsupported or constrained behavior:
DatabaseExceptionbecause SQLite does not support row-levelSELECT ... FOR UPDATE.DatabaseExceptionwhenlockForUpdate()is combined withlimit()oroffset(), because Oracle does not allowFOR UPDATEwith row limiting.DatabaseExceptionwhen used without aFROMtable or on subqueries.Notes
This PR intentionally keeps the first implementation narrow. It does not add
sharedLock(),skipLocked(),nowait()or SQLSRV joined-table hinting.Those could be considered later, but this PR focuses only on the most common pessimistic write-lock use case.
Testing
Added Query Builder coverage for:
FOR UPDATEcompilation$reset = falsebehaviorcountAllResults()suppressing lock clausesFROMqueriesChecklist